/* Copyright (c) 2011 by Mani Brundha Kattamuri, Haritha Subramanyam
 *
 * Project Info:  http://code.google.com/p/andriod-travelapp-csc780/wiki/Design
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */
package com.example.TravelApp.search.advanced;

import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import com.example.TravelApp.R;
import com.example.TravelApp.constants.MapConstants;
import com.example.TravelApp.search.common.PlaceObject;
import com.example.TravelApp.search.common.PlotItemizedOverlay;
import com.example.TravelApp.search.common.helper.MapDrawPathHelper;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.MapView;

/**
 * This activity is called when the user selects a venue address The layout is
 * described in file <code>res/layout/display_list_route.xml</code>. This
 * activity displays the complete route between all the venue locations; It uses
 * Google Maps API for the route and location co-ordinates.
 * 
 * @author Mani Brundha and Haritha Subramanyam
 * 
 */
public class AdvancedMapRouteActivity extends
		com.google.android.maps.MapActivity implements MapConstants {

	private MapView mapView;
	private ArrayList<PlaceObject> list;
	private String place;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.display_list_route);
		Intent myIntent = getIntent();
		this.list = myIntent.getParcelableArrayListExtra("placesList");
		place = (String) myIntent.getCharSequenceExtra("Place");
		if (this.list.isEmpty() != true) {
			poltAndDrawPath();
		} else {
			// Displays No co-ordinates
			setContentView(R.layout.no_results);
		}
	}

	// @Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	/**
	 * Creates a mapview and draws the route between two consecutive points for
	 * the list of locations sent through the intent. The route between the
	 * points is retrieved using Google Maps API. A path is first drawn using
	 * drawPath method in MapDrawPathHelper class After the path is drawn, a set
	 * of overlays are placed on each of the locations.
	 */
	private void poltAndDrawPath() {
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		GeoPoint srcGeoPoint;
		GeoPoint destGeoPoint;
		int curColor = Color.GREEN;
		// Handle start to first location separately
		if (MapDrawPathHelper.isNotNull(list.get(0).getLatitudeAndLongitude()) == true) {
			srcGeoPoint = MapDrawPathHelper.getGeoPoint(place);
			destGeoPoint = MapDrawPathHelper.getGeoPoint(list.get(0)
					.getLatitudeAndLongitude());
			curColor = getCurColor(true, list.get(0).getIsVisited());
			MapDrawPathHelper.drawPath(srcGeoPoint, destGeoPoint, curColor,
					mapView);
			setOverlays(-1, srcGeoPoint, destGeoPoint);
		}
		for (int i = 1; i < list.size(); i++) {
			// First check for Null Pointers; proceeds only if the start
			// co-ordinates are available
			if (MapDrawPathHelper.isNotNull(list.get(i - 1)
					.getLatitudeAndLongitude()) == true) {
				// construct a geopint out of the string
				srcGeoPoint = MapDrawPathHelper.getGeoPoint(list.get(i - 1)
						.getLatitudeAndLongitude());

				// First check for Null Pointers; proceeds only if the start
				// co-ordinates are available
				if (MapDrawPathHelper.isNotNull(list.get(i)
						.getLatitudeAndLongitude()) == true) {
					destGeoPoint = MapDrawPathHelper.getGeoPoint(list.get(i)
							.getLatitudeAndLongitude());
					curColor = getCurColor(list.get(i - 1).getIsVisited(), list
							.get(i).getIsVisited());
					// draw path between two consecutive points
					MapDrawPathHelper.drawPath(srcGeoPoint, destGeoPoint,
							curColor, mapView);
					// Plot the overlays for all the locations
					setOverlays(i, srcGeoPoint, destGeoPoint);
				}
			}
		}
		mapView.getController().setZoom(15);

	}

	/**
	 * Places overlays on all the locations of places of interest in the list
	 * This method is called after the path is drawn between two points
	 * 
	 * @param index
	 *            - index of the item in the list of places sent through intent
	 * @param src
	 *            - From location GeoPoint
	 * @param dest
	 *            - To location GeoPoint
	 */
	private void setOverlays(int index, GeoPoint src, GeoPoint dest) {
		List<Overlay> mapOverlays = mapView.getOverlays();
		Drawable drawable = this.getResources().getDrawable(
				R.drawable.map_pin_green);
		PlotItemizedOverlay itemizedoverlay = new PlotItemizedOverlay(drawable,
				this);

		// Handle start to first location separately
		if (index == -1) {
			OverlayItem overlayitemFinal = new OverlayItem(src, "Start", "0.0");
			itemizedoverlay.addOverlay(overlayitemFinal);
			mapOverlays.add(itemizedoverlay);
			mapView.getController().animateTo(dest);
		} else {// other locations from the places list
			OverlayItem overlayitem = new OverlayItem(src,
					list.get(index - 1).getVenueName() + "\n\n"
							+ list.get(index - 1).getAddress(), list.get(
							index - 1).getRating());
			itemizedoverlay.addOverlay(overlayitem);
			mapOverlays.add(itemizedoverlay);
			mapView.getController().animateTo(src);
		}
		// Final Destination location
		if (index == list.size() - 1) {
			OverlayItem overlayitemFinal = new OverlayItem(dest, list
					.get(index).getVenueName()
					+ "\n\n"
					+ list.get(index).getAddress(), list.get(index).getRating());
			itemizedoverlay.addOverlay(overlayitemFinal);
			mapOverlays.add(itemizedoverlay);
			mapView.getController().animateTo(dest);
		}

	}

	/**
	 * Draws the route when the activity resumes as the list gets updated with
	 * the places already visited
	 */
	public void onResume() {
		poltAndDrawPath();
		super.onResume();
	}

	/**
	 * Color codes the routes based on whether the user has visited the places
	 * If the use has already visited 2 places then the route connecting is
	 * green If the user has visited one place on the route connecting two
	 * places, it is colored in blue If the entire list of places are not
	 * visited, then it is in grey
	 * 
	 * @param isVisitedA
	 *            - the first location on the route
	 * @param isVisitedB
	 *            - the final location on the route
	 * @return color - Default color red
	 */
	public int getCurColor(boolean isVisitedA, boolean isVisitedB) {
		if (isVisitedA == true && isVisitedB == false)
			return Color.GREEN;
		else if (isVisitedA == true && isVisitedB == true)
			return Color.BLUE;
		else if (isVisitedA == false && isVisitedB == false)
			return Color.GRAY;

		return Color.RED;
	}
}
